package com.atguigu.edu.publisher.controller;

import com.atguigu.edu.publisher.beans.UserChangeCtPerType;

import com.atguigu.edu.publisher.service.UserStatsService;
import com.atguigu.edu.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
//@RequestMapping("/edu/realtime/user")
public class UserStatsController {

    @Autowired
    private UserStatsService userStatsService;

    @RequestMapping("/userCt")
    public String getUserChange(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<UserChangeCtPerType> userChangeCtList = userStatsService.getUserChangeCt(date);
        /*if (userChangeCtList == null) {
            return "";
        }*/
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": {\"columns\": [{\"name\": \"变动类型\",\"id\": \"type\"},{\"name\": \"用户数\",\"id\": \"user_ct\"}],\"rows\": [");
        for (int i = 0; i < userChangeCtList.size(); i++) {
            UserChangeCtPerType userChangeCtPerType = userChangeCtList.get(i);
            jsonB.append("{\"type\": \""+userChangeCtPerType.getType()+"\",\"user_ct\": \""+userChangeCtPerType.getUserCt()+"\"}");
            if (i < userChangeCtList.size() - 1) {
                jsonB.append(",");
            }
        }
        jsonB.append("]}}");
        return jsonB.toString();

    }
}
